<template>
    <!-- 表单展示-->
    <el-form inline="true">
        <el-form-item>
            <selector v-model="params.difficulty" placeholder="请选择难度" style="width: 200px;"></selector>
        </el-form-item>
        <el-form-item>
            <el-input v-model="params.title" placeholder="请输⼊题⽬" />
        </el-form-item>
        <el-form-item>
            <el-button plain @click = "onSearch" >搜索</el-button>
            <el-button plain type="info" @click = "onReset" >重置</el-button>
            <el-button plain type="primary" :icon="Plus" @click="onAddQuestion">添加题⽬</el-button>
        </el-form-item>
    </el-form>
    <!-- 列表展示-->
    <el-table height="526px" :data="questionList">
        <el-table-column type="index" align="center" label="序号" width="60px" />
        <el-table-column prop="questionId" width="190px" label="题⽬id" />
        <el-table-column prop="title" label="题⽬标题" />
        <el-table-column prop="difficulty" label="题⽬难度" width="90px">
            <template #default="{ row }">
                <div v-if="row.difficulty === 1" style="color:#3EC8FF;">简单</div>
                <div v-if="row.difficulty === 2" style="color:#FE7909;">中等</div>
                <div v-if="row.difficulty === 3" style="color:#FD4C40;">困难</div>
            </template>
        </el-table-column>
        <el-table-column prop="createName" label="创建⼈" width="140px" />
        <el-table-column prop="createTime" label="创建时间" width="180px" />
        <el-table-column label="操作" width="100px" fixed="right">
            <!-- 添加插槽 获取当前行的数据-->
            <template #default="{ row }">
                <el-button type="text" @click="onEdit(row.questionId)">编辑
                </el-button>
                <el-button type="text" class="red" @click="onDelete(row.questionId)">删除
                </el-button>
            </template>
        </el-table-column>
    </el-table>
    <!-- 分页展示-->
    <el-pagination 
        background 
        size="small" 
        layout="total, sizes, prev, pager, next, jumper" 
        :total="total"
        :page-sizes="[5, 10, 15, 20]" 
        v-model:current-page="params.pageNum"
        v-model:page-size="params.pageSize"
        @size-change="handleSizeChange"       
        @current-change="handleCurrentChange"
    />

    <question-drawer ref="questionDrawerRef" @success="onSuccess"></question-drawer>
</template>

<script setup>
import { Plus } from "@element-plus/icons-vue"
import Selector from "@/components/QuestionSelector.vue"
import { getQuestionListService, delQuestionService} from "@/api/question"
import { reactive, ref } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import QuestionDrawer from "@/components/QuestionDrawer.vue"


const params = reactive({
    pageNum: 1,
    pageSize: 10,
    difficulty: null,
    title: null
})

//定义响应式数据
const questionList = ref([])
const total = ref(0);


async function getQuestionList() {
    const result = await getQuestionListService(params)
    console.log(result)
    questionList.value = result.rows
    total.value = result.total;
}
getQuestionList();

function handleSizeChange(newPage) {
    params.pageNum = 1;
    getQuestionList();
}

function handleCurrentChange(newNum) {
    getQuestionList();
}

function onSearch(){
    params.pageNum=1
    getQuestionList()
}

function onReset() {
    params.pageNum = 1
    params.pageSize = 10
    params.title = null
    params.difficulty = null
    getQuestionList();
}

const questionDrawerRef = ref()

function onAddQuestion() {
    questionDrawerRef.value.open()
}

function onSuccess(service) {
    if(service === 'add') {
        params.pageNum = 1
    }
    getQuestionList()
}

async function onEdit(questionId) {
    questionDrawerRef.value.open(questionId)
}

async function onDelete(questionId) {
    try {
    await ElMessageBox.confirm(
      '确定要删除这条记录吗？',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
    // 用户点击确定后才会走到这里
    await delQuestionService(questionId)
    ElMessage.success('删除成功')
    params.pageNum = 1
    getQuestionList()
  } catch (error) {
    // 如果用户点取消，或关闭弹窗，会进到这里
    ElMessage.info('已取消删除')
  }
}

</script>